<template>
  <div>
    <el-form ref="deptForm" :model="form" :rules="rules" label-width="120px">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="form.name" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="form.code" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="form.manager" style="width:80%" placeholder="请选择">
          <el-option v-for="item in employees" :key="item.id" :value="item.username" :label="item.username" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="form.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="small" @click="hSubmit">确定</el-button>
        <el-button size="small" @click="hCancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getEmployeesSimple } from '@/api/employees'
import { addDepartments, getDepartmentsDetail, updateDepartments } from '@/api/departments'
export default {
  props: {
    id: {
      type: String,
      required: true
    },
    isEdit: {
      type: Boolean,
      required: true
    },
    originList: {
      type: Array,
      required: true
    }
  },
  data() {
    const validName = (rule, value, callback) => {
      let nameList = this.originList
      // 拿到与该点击的id相同的那个对象的pid
      const pid = nameList.find(item => item.id === this.id).pid
      // console.log(pid)
      if (this.isEdit) {
        // 找打兄弟部门不包括自己
        nameList = this.originList.filter(item => item.pid === pid && item.id !== this.id)
        nameList.some(item => item.name === value) ? callback(new Error(value + '已占有')) : callback()
      } else {
        // 找到同级部门
        nameList.filter(item => item.pid === this.id).some(item => item.name === value) ? callback(new Error(value + '已占有')) : callback()
        // const res = nameList.filter(item => item.pid === this.id)
        // console.log(res)
      }
    }

    const validCode = (rule, value, callback) => {
      if (this.isEdit) {
        this.originList.filter(item => item.id !== this.id).some(item => item.code === value) ? callback(new Error(value + '已占有')) : callback()
      } else {
        // this.originList.some(item => item.code === value) ? callback(new Error(value + '已占有')) : callback()
        const codeList = this.originList.map(item => item.code)
        if (codeList.includes(value)) {
          callback(new Error(value + '已占用'))
        } else {
          callback()
        }
      }
    }
    return {
      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: 'blur' },
          { validator: validName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          { validator: validCode, trigger: 'blur' },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '部门负责人不能为空', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { min: 1, max: 300, message: '部门介绍要求1-300个字符', trigger: 'blur' }
        ]
      },
      employees: []
    }
  },
  created() {
    this.loadEmployeesSimple()
    this.loadDetail()
  },
  methods: {
    async loadEmployeesSimple() {
      try {
        const res = await getEmployeesSimple()
        // console.log(res)
        this.employees = res.data
      } catch (e) {
        this.$message.error(e.message)
      }
    },
    async loadDetail() {
      if (this.isEdit) {
        try {
          const res = await getDepartmentsDetail(this.id)
          // console.log(res)
          this.form = { ...res.data }
        } catch (e) {
          this.$message.error(e.message)
        }
      }
    },
    async hSubmit() {
      const valid = await this.$refs.deptForm.validate().catch(e => e)
      if (!valid) return
      this.isEdit ? this.doEdit() : this.doAdd()
    },
    async doAdd() {
      try {
        const parmas = { ...this.form, pid: this.id }
        const res = await addDepartments(parmas)
        // console.log(res)
        this.$message.success(res.message)
        // 通知父组件关键dialog
        this.$emit('success')
      } catch (e) {
        this.$message.error(e.message)
      }
    },
    async doEdit() {
      try {
        const res = await updateDepartments(this.form)
        // console.log(res)
        this.$message.success(res.message)
        // 通知父组件关键dialog
        this.$emit('success')
      } catch (e) {
        this.$message.error(e.message)
      }
    },
    hCancel() {
      this.$emit('close')
      this.$refs.deptForm.resetFields()
    }
  }
}

</script>

<style lang="scss" scoped>

</style>
